import re,urllib2,os,sys
IGNORE_EXISTING_FILES=True

def google_code_download(url,dir):
	p_url=re.compile(r'<a href="(?P<url>[^>]+)">',re.I)
	page=urllib2.urlopen(url).read()
	m_url=p_url.findall(page)
	if not os.path.exists(dir):os.makedirs(dir)
	for i in m_url:
		if i.startswith('http://') or i=='../':
			continue
		elif i.endswith('/'):
			new_url,new_dir=url+'/'+i,dir+'/'+i
			google_code_download(new_url,new_dir)
		else:
			dest_url,dest_file=url+'/'+i,dir+'/'+i
			if IGNORE_EXISTING_FILES and os.path.exists(dest_file):
				continue
			with open(dest_file,'wb') as local_file:
				remote_file=urllib2.urlopen(dest_url)
				local_file.write(remote_file.read())

def main():
	if len(sys.argv)!=3:
		sys.exit('Usage: {0} trunk_url dest_folder'.format(sys.argv[0]))
	trunk_url,dest_folder=sys.argv[1],sys.argv[2]
	google_code_download(trunk_url,dest_folder)
		
main()
# Example::
# D:\GoogleCodeBatch.py http://demohelper.googlecode.com/svn/trunk/ R:/DemoHelper